package cn.cmcc.online.smsapi.sdk.util;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import cn.cmcc.online.smsapi.sdk.db.ConversationDBManager;
import cn.cmcc.online.smsapi.sdk.db.DBManager;
import cn.cmcc.online.smsapi.sdk.db.XyCursor;
import cn.cmcc.online.smsapi.sdk.dex.DexUtil;
import cn.cmcc.online.smsapi.sdk.log.LogManager;

public class ConversationManager {

    /*SDK-382 /songzhirong/2016.05.17---start---*/
    /**
     * 
     * @param ctx
     * @param actionType 
     * @param data
     * @param extend
     * @return
     */
	public static JSONObject queryConversationMsg(Context ctx,String actionType, JSONObject data, Map extend){
    	return DexUtil.queryConversationMsg( ctx,actionType,  data,  extend);
    }    
    
	/**
	 * 插入数据库
	 * @param tableName 表名
	 * @param values 插入的值
	 * @return
	 */
	public static long insert(String tableName, ContentValues values) {
		try {
			return ConversationDBManager.insert(tableName, values);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		return -1;
	}

	/**
	 * 更新数据库
	 * @param tableName 表名
	 * @param values 更新值
	 * @param whereClause where条件
	 * @param whereArgs where参数
	 * @return
	 */
	public static int update(String tableName, ContentValues values,
			String whereClause, String[] whereArgs) {
		try {
			return ConversationDBManager.update(tableName, values, whereClause,
					whereArgs);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		return -1;

	}

	/**
	 * 删除
	 * @param tableName 表名
	 * @param whereClause where条件
	 * @param whereArgs where参数
	 * @return
	 */
	public static int delete(String tableName, String whereClause,
			String[] whereArgs) {
		try {
			return ConversationDBManager.delete(tableName, whereClause,
					whereArgs);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		return -1;
	}

	/**
	 * 查询
	 * @param tableName 表名
	 * @param columns 返回的列
	 * @param selection 条件
	 * @param selectionArgs 参数
	 * @return
	 */
	public static XyCursor query(String tableName, String[] columns,
			String selection, String[] selectionArgs) {
		try {
			return ConversationDBManager.query(tableName, columns, selection,
					selectionArgs);

		} catch (Throwable e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 
	 * @param distinct 是否消除重复行
	 * @param tableName 表名
	 * @param columns 返回的列
	 * @param selection 条件
	 * @param selectionArgs  参数
	 * @param groupBy 
	 * @param having
	 * @param orderBy
	 * @param limit
	 * @return
	 */
	public static XyCursor query(boolean distinct, String tableName,
			String[] columns, String selection, String[] selectionArgs,
			String groupBy, String having, String orderBy, String limit) {
		try {
			return ConversationDBManager.query(distinct, tableName, columns,
					selection, selectionArgs, groupBy, having, orderBy, limit);

		} catch (Throwable e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param groupBy
	 * @param having
	 * @param orderBy
	 * @param limit
	 * @return
	 */
	public static XyCursor query(String tableName, String[] columns,
			String selection, String[] selectionArgs, String groupBy,
			String having, String orderBy, String limit) {

		try {
			return ConversationDBManager.query(tableName, columns, selection,
					selectionArgs, groupBy, having, orderBy, limit);

		} catch (Throwable e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 直接执行sql
	 * @param sql
	 */
	public static void execSQL(final String sql) {
		ConversationDBManager.execSQL(sql);
	}
    /*SDK-431 songzhirong 20160530 start*/
	/**
	 * 同步执行sql
	 * @param sql
	 */
	public static void execSQLSyn(final String sql) {
        ConversationDBManager.execSQLSyn(sql);
    }
    /*SDK-431 songzhirong 20160530 end*/

	/**
	 * 
	 * @param sql sql语句
	 * @param selectionArgs 选择参数
	 * @return
	 * @throws Exception
	 */
	public static XyCursor rawQuery(String sql, String[] selectionArgs)
			throws Exception {
		try {
			return ConversationDBManager.rawQuery(sql, selectionArgs);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 
	 * @param tableName 表名
	 * @param columns 列名
	 * @param selection 条件
	 * @param selectionArgs 条件参数
	 * @return
	 */
	public static XyCursor queryDBManager(String tableName, String[] columns,
			String selection, String[] selectionArgs) {
		try {
			return DBManager.query(tableName, columns, selection,
					selectionArgs);

		} catch (Throwable e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 
	 * @param distinct
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param groupBy
	 * @param having
	 * @param orderBy
	 * @param limit
	 * @return
	 */
	public static XyCursor queryDBManager(boolean distinct, String tableName,
			String[] columns, String selection, String[] selectionArgs,
			String groupBy, String having, String orderBy, String limit) {
		try {
			return DBManager.query(distinct, tableName, columns,
					selection, selectionArgs, groupBy, having, orderBy, limit);

		} catch (Throwable e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 
	 * @param tableName
	 * @param columns
	 * @param selection
	 * @param selectionArgs
	 * @param groupBy
	 * @param having
	 * @param orderBy
	 * @param limit
	 * @return
	 */
	public static XyCursor queryDBManager(String tableName, String[] columns,
			String selection, String[] selectionArgs, String groupBy,
			String having, String orderBy, String limit) {

		try {
			return DBManager.query(tableName, columns, selection,
					selectionArgs, groupBy, having, orderBy, limit);

		} catch (Throwable e) {
			// TODO: handle exception
		}
		return null;
	}
	
	/**
	 * 
	 * @param sql
	 * @param selectionArgs
	 * @return
	 * @throws Exception
	 */
	public static XyCursor rawQueryDBManager(String sql, String[] selectionArgs)
			throws Exception {
		try {
			return DBManager.rawQuery(sql, selectionArgs);
		} catch (Throwable e) {
			// TODO: handle exception
		}
		return null;
	}
    
    /*SDK-382 /songzhirong/2016.05.17---end---*/
	
    /*SDK-431 songzhirong 20160530 start*/
    public static void saveLogIn(final String uuid,String className, String methodName,
			Object... args) {
//        LogManager.d("saveLogIn", "className="+className+"methodName="+methodName);
    	DexUtil.saveLogIn(uuid,className, methodName, args);
    }
    
    public static void saveLogOut(final String uuid,String className, String methodName,
 			Object... args) {
//        LogManager.d("saveLogOut", "className="+className+"methodName="+methodName);
     	DexUtil.saveLogOut(uuid,className, methodName, args);
     }       
    
    public static JSONArray rawQueryJSONArray(String sql){
    	return ConversationDBManager.rawQueryJSONArray(sql);
    }

    /**
     * 
     * @param tableName 不能为空
     * @param sql 可用sql查询
     * @param valuesObject 也可以是键值对插入
     */
    public static void insertLog(String tableName,String sql,JSONObject valuesObject){		
		DexUtil.insertLog(tableName, sql, valuesObject);
	}
	
    /**
     * 
     * @param tableName 不能为空
     * @param sql 可用sql删除
     * @param whereArgs 可以用where条件删除
     */
	public static void deleteLog(String tableName,String sql,JSONObject whereArgs){
		DexUtil.deleteLog(tableName, sql, whereArgs);		
	}
	
	/**
	 * 
	 * @param tableName 不能为空
	 * @param sql  可用sql查询
	 * @param columns 需要查询的columns
	 * @param whereArgs 查询的键值对
	 * @param orderBy 
	 * @param limit
	 * @return
	 */
	public static JSONArray queryLog(String tableName,
			String sql,String[] columns, JSONObject whereArgs,String orderBy, String limit){
		return DexUtil.queryLog(tableName,  sql, columns, whereArgs, orderBy, limit);
	}
    /*SDK-431 songzhirong 20160530 end*/


}
